package com.zdkj.subway.epm.util;/**
 * @author 陈博
 * @date 2020/6/2 0002 14:54
 */

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @program: zdkj
 *
 * @description: LocalDateTime数据格式转换
 *
 * @author: chen
 *
 * @create: 2020-06-02 14:54
 **/
public class LocalDateTimeConverter implements Converter<LocalDateTime> {

    @Override
    public Class<LocalDateTime> supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }
    /*
     * 转换为Java格式(non-Javadoc)
     * @see com.alibaba.excel.converters.Converter#convertToJavaData(com.alibaba.excel.metadata.CellData, com.alibaba.excel.metadata.property.ExcelContentProperty, com.alibaba.excel.metadata.GlobalConfiguration)
     */
    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                           GlobalConfiguration globalConfiguration) {
        return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /*
     * 转换为excel格式(non-Javadoc)
     * @see com.alibaba.excel.converters.Converter#convertToExcelData(java.lang.Object, com.alibaba.excel.metadata.property.ExcelContentProperty, com.alibaba.excel.metadata.GlobalConfiguration)
     */
    @Override
    public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

}

